<template>
  <div>
    <!--<h1>支付页面</h1>-->

    <!--<button @click="pay">支付测试</button>-->
  </div>
</template>

<script>
export default {
  data(){
    return{

    }
  },
  created(){
    if (window.sessionStorage) {
      alert("浏览器支持sessionStorage");
    }else {
      alert("浏览器不支持sessionStorage");
    }
  },
  methods:{
    pay(id){
      let out_trade_no='2018'+Math.random().toString().substr(2, 10);
      this.axios.get('/api/wxpay',{params:{
        uid:id,
        taskid:this.$route.query.taskid,
        body:'文字',
        out_trade_no:out_trade_no,
        fee:1,
        openid:'obV1T5zupOjlONbJz_zCZvYXCLBI'
      }}).then(result=>{
         function onBridgeReady() {
          window.WeixinJSBridge.invoke(
            "getBrandWCPayRequest",
            {
              appId: result.data.appId,
              timeStamp: result.data.timeStamp,
              nonceStr: result.data.nonceStr,
              package: result.data.package,
              signType: result.data.signType,
              paySign: result.data.paySign
            },

            function(res) {
              if (res.err_msg == "get_brand_wcpay_request:ok") {
                // 使用以上方式判断前端返回,微信团队郑重提示：
                //res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
                alert("ok");
                alert(JSON.stringify(res));
              } else {
                alert(JSON.stringify(res));
              }
            }
          );
        }
        if (typeof WeixinJSBridge == "undefined") {
          if (document.addEventListener) {
            document.addEventListener(
              "WeixinJSBridgeReady",
              onBridgeReady,
              false
            );
          } else if (document.attachEvent) {
            document.attachEvent("WeixinJSBridgeReady", onBridgeReady);
            document.attachEvent("onWeixinJSBridgeReady", onBridgeReady);
          }
        } else {
          onBridgeReady();
        }
      })
    }
  }
}
</script>
